<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog v-if="props.visible" v-model="props.visible" :title="props.operateTitle" width="700px" append-to-body :before-close="closeDialog">
    <div v-loading="loading">
      <el-form ref="formRef" :model="formData" :rules="rules" size="small" label-width="120">
        <el-form-item v-if="dialogType == '0'" label="业务场景名称：" prop="name">
          <el-input v-model="formData.name" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item v-if="dialogType == '0'" label="业务场景描述：" prop="description">
          <el-input type="textarea" rows="3" v-model="formData.description" maxlength="2000"></el-input>
        </el-form-item>
        <el-form-item v-if="dialogType == '1'" label="所属业务场景：" prop="parentId">
          <el-select :disabled="(props?.parentId && props?.parentId != 0) || props.operateType == 'DETAIL'" style="width: 480px" v-model="formData.parentId" clearable placeholder="请选择">
            <el-option v-for="item in businessList" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogType == '1'" label="业务系统名称：" prop="name">
          <el-input v-model="formData.name" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item v-if="dialogType == '1'" label="业务系统描述：" prop="description">
          <el-input type="textarea" rows="3" v-model="formData.description" maxlength="2000"></el-input>
        </el-form-item>
        <el-form-item v-if="operateType !== 'ADD'" label="创建人：" prop="createUserNickName">
          <span>{{ formData.createUserNickName }}</span>
        </el-form-item>
        <el-form-item v-if="operateType !== 'ADD'" label="创建时间：" prop="createTime">
          <span>{{ formData.createTime }}</span>
        </el-form-item>
      </el-form>
    </div>
    <template v-if="props.operateType !== 'DETAIL'" #footer>
      <div class="dialog-footer" style="text-align: center;">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button v-if="props.operateType !== 'View'" size="small" type="primary" @click="saveForm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="BusinessSituationOperate">
const typeMap = {'0': '业务场景', '1': '业务系统'}
import { addBusinessSystem, updateBusinessSystem, getBusinessSystemDetail, getBusinessSystemList } from '@/api/businessSystem';
const emit = defineEmits(['handleEvent']);
const loading = ref(false);
const props = defineProps(['visible', 'row', 'operateType', 'dialogType', 'operateTitle', 'parentId']);
const formData = ref({
  name: '',
  description: '',
  type: Number(props.dialogType),
  parentId: null
})
const formRef = ref();
const businessList = ref([])
const rules = ref({
  parentId: [
    { required: true, message: '请选择业务场景', trigger: 'change' },
  ],
  name: [          
    { required: true, message: `请输入${props.dialogType == '0' ? '业务场景' : '业务系统'}名称`, trigger: 'change' },                           
  ],
  description: [          
    { required: true, message: `请输入${props.dialogType == '0' ? '业务场景' : '业务系统'}描述`, trigger: 'change' },                           
  ]
}); 
const closeDialog = () => {
  emit('handleEvent', {type: 'cancel'});
}
const saveForm = async () => {
  let flag = await formRef.value.validate();
  if (flag) {
    let ApiRequest = props.operateType == 'ADD' ? addBusinessSystem : updateBusinessSystem
    let typeName = typeMap[props.dialogType];
    if (props.dialogType != '1') {
      formData.value.parentId = 0;
    } 
    ApiRequest(formData.value).then(res => {
      if (res) {
        ElMessage({ showClose: true, message: props.operateType == 'ADD' ? `${typeName}新增成功` : `${typeName}修改成功`, type: 'success' });
        emit('handleEvent', {type: 'refresh'});
      }
    })
  }
}
const getDetail = (id) => {
  getBusinessSystemDetail({id}).then(res => {
    if (res) {
      formData.value = res.data;
      props?.parentId && props?.parentId != 0 && (formData.value.parentId = props?.parentId);
    }
  })
}
const getEnumList = async () => {
  await getBusinessSystemList({pageSize: 9999, pageNum: 1}).then(res => {
    if (res.rows) {
      businessList.value = res.rows.filter(item => item.type == '0');
    }
  })
}
onMounted(async () => {
  props.dialogType == 1 && await getEnumList(); 
  if (props?.row?.id) {
    getDetail(props?.row?.id);
  } else {
    props?.parentId && props?.parentId != 0 && (formData.value.parentId = props?.parentId);
  }
});
</script>

<style lang="scss" scoped>

</style>
